﻿using Anke.SHManage.BLL.FixedAssetsBLL;
using Anke.SHManage.Model.FixedAssetsModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Anke.SHManage.Web.Areas.FixedAssets.Controllers
{
    public class FixedAssetsRepairController : Controller
    {
        // 固定资产维修
        // GET: /FixedAssets/FixedAssetsRepair/

        public ActionResult AssetsRepairList()
        {
            return View("AssetsRepairList");
        }

        /// <summary>
        /// 获取固定资产维修记录列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetRepairList()
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求的页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //获取查询条件
            string assetTypeID = Request.Form["assetTypeID"]; //资产分类编码
            string materialID = Request.Form["materialID"];   //资产编码
            string assetID = Request.Form["assetID"]; //固定资产编号
            string startDate = Request.Form["startDate"];//起始日期
            string endDate = Request.Form["endDate"];//截止日期
            DateTime DateStart = Convert.ToDateTime(startDate);//日期
            DateTime DateEnd = Convert.ToDateTime(endDate).AddDays(1); //日期

            FixedAssetsRepairBLL bll = new FixedAssetsRepairBLL();
            var list = bll.GetRepairList(pageSize, pageIndex, assetTypeID, materialID, assetID, DateStart, DateEnd);
            return Json(list, "appliction/json", JsonRequestBehavior.AllowGet);

        }

        #region 新增/编辑

        /// <summary>
        /// 固定资产维修新增/编辑视图页
        /// </summary>
        /// <param name="State"></param>
        /// <param name="Code"></param>
        /// <returns></returns>
        public ActionResult RepairInfoEdit(string State, string Code)
        {
            ViewBag.State = State;
            ViewBag.Code = Code;
            return View("RepairInfoEdit");
        }

        /// <summary>
        /// 根据维修记录编码获取对应资产维修数据
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public ActionResult GetRepairData(string Code)
        {
            FixedAssetsRepairBLL bll = new FixedAssetsRepairBLL();
            FIXED_RepairInfo info = bll.GetRepairData(Code);
            return Json(info, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 保存新增/编辑
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public ActionResult SaveEditPageData(FIXED_RepairInfo info)
        {
            string str = "";
            string state = Request.Form["state"].ToString();
            FixedAssetsRepairBLL bll = new FixedAssetsRepairBLL();
            switch (state)
            {
                case "edit":
                    if (bll.UpdateEditPageData(info))
                    {
                        str = "更新成功";
                    }
                    else
                    {
                        str = "更新失败";
                    }
                    break;
                case "new":
                    if (bll.InsertEditPageData(info))
                    {
                        str = "新增成功";
                    }
                    else
                    {
                        str = "新增失败";
                    }

                    break;
            }
            return Json(str, "text/html", JsonRequestBehavior.AllowGet);
        }

        #endregion
    }
}
